package com.nwu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nwu.entities.academicPaper;
import com.nwu.entities.paperForm;
import com.nwu.entities.vo.ThesisQuery;
import com.nwu.service.ThesisManagementService;
import com.nwu.util.Result;
import com.nwu.util.resultCode.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  增删改查接口
 * </p>
 *
 * @author wangb
 * @since 2022-04-11
 * @since 2022-4-15
 */
//http://localhost:8080/ResearchProjectCollectionSpringBoot/getInfo/findAll

@Api(tags = "论文期刊管理")
@RestController
@RequestMapping("/ResearchProjectCollectionSpringBoot/getInfo")
public class ThesisManagementController {
    @Autowired
    private ThesisManagementService thesisManagementService;
    //查询所有信息


    @ApiOperation("查询所有论文信息")
    @GetMapping("findAll")
    public R findAllInfo(){
        List<paperForm> list = thesisManagementService.list(null);
        return R.ok().data("items",list);
    }

    @ApiOperation("逻辑删除论文信息")
    @DeleteMapping("{id}")
    public R removePaper(@PathVariable Long id){
        boolean flag = thesisManagementService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //分页查询          current 当前页，limit记录数
    @ApiOperation("分页查询论文信息")
    @GetMapping("pageFind/{current}/{limit}")
    public R pageList(@PathVariable long current,
                           @PathVariable long limit){
        //创建page对象
        Page<paperForm> pageSciResearch =new Page<>(current,limit);
        thesisManagementService.page(pageSciResearch,null);
        long total = pageSciResearch.getTotal();   //总记录数
        List<paperForm> records = pageSciResearch.getRecords();

        Map map =new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }

    //条件查询带分页
    @ApiOperation("带条件的分页查询论文信息")
    @PostMapping("pageConditionFind/{current}/{limit}")
    public R pageConditionFind(@PathVariable long current, @PathVariable long limit,
                                    @RequestBody(required = false) ThesisQuery thesisQuery){

        //创建page对象，传递当前页和每页记录数
        Page<paperForm> pageSciResearch =new Page<>(current,limit);
        QueryWrapper<paperForm> wrapper=new QueryWrapper<>();
        //wrapper构建条件,多条件组合查询 动态sql，判断条件是否为空，如果不为空拼接条件

        String author_id = thesisQuery.getAuthor_id();
        String paper_name = thesisQuery.getPaper_name();
        String paper_date = thesisQuery.getPaper_date();
        String periodical_name = thesisQuery.getPeriodical_name();
        String paper_subject = thesisQuery.getPaper_subject();
        String periodical_language = thesisQuery.getPeriodical_language();
        if(!StringUtils.isEmpty(author_id)){
            wrapper.eq("author_id",author_id);
        }
        if(!StringUtils.isEmpty(paper_name)){
            wrapper.like("paper_name",paper_name);
        }
        if(!StringUtils.isEmpty(periodical_name)){
            wrapper.like("periodical_name",periodical_name);
        }
        if(!StringUtils.isEmpty(paper_date)){
            wrapper.eq("paper_date",paper_date);
        }
        if(!StringUtils.isEmpty(paper_subject)){
            wrapper.eq("paper_subject",paper_subject);
        }
        if(!StringUtils.isEmpty(periodical_language)){
            wrapper.eq("periodical_language",periodical_language);
        }


        thesisManagementService.page(pageSciResearch, wrapper);

        long total = pageSciResearch.getTotal();   //总记录数
        List<paperForm> records = pageSciResearch.getRecords(); //数据list集合

        return R.ok().data("total",total).data("rows",records);

    }

    //教师添加论文信息
    @ApiOperation("添加论文信息")
    @PostMapping("addThesisManagement")
    public R addSciManagement(@RequestBody paperForm paperForm){
        paperForm.setIsDeleted(0);
        boolean save = thesisManagementService.save(paperForm);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }

    }
    //根据科研项目id进行查询
    @ApiOperation("根据论文id查询")
    @GetMapping("GetThesis/{paper_id}")
    public String getThesis(@PathVariable String paper_id){
        paperForm paper = thesisManagementService.getById(paper_id);
          return Result.ok("paper",paper);
      }

    //科研信息修改
    @ApiOperation("论文信息修改")
    @PostMapping("updateThesis")
    public R updateThesis(@RequestBody paperForm paperForm){
        boolean flag = thesisManagementService.updateById(paperForm);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    }

